//
//  TileViewCell.m
//  etionUI
//
//  Created by wangjian on 7/23/13.
//
//

#import "TileWallCellView.h"

#define DURATION .5

@implementation CTileWallCellView

@synthesize placeHolderView = _placeHolderView;
@synthesize tileView = _tileView;

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        UIView *placeHolderView = [[UIView alloc] initWithFrame:self.bounds];
        placeHolderView.backgroundColor = [UIColor clearColor];
        [self addSubview:placeHolderView];
        self.placeHolderView = placeHolderView;
        [placeHolderView release];
    }
    return self;
}

- (void)dealloc
{
    self.placeHolderView = nil;
    self.tileView = nil;
    [super dealloc];
}
//
//- (void)setTileView:(CTileView *)tileView
//{
//    CTileView *tmp = [tileView retain];
//    [_tileView release];
//    _tileView = tmp;
//
//    _tileView.frame = self.bounds;
//}

- (void)relpeceTileView:(CTileView *)tileView afterDelay:(NSTimeInterval)delayTime complete:(void (^)(BOOL isFinished))complete
{
    if (self.tileView != nil)
    {
        if (tileView != nil)    //replace
        {
            DispatchMainThreadSyncAfterDelay(delayTime, ^{
                [UIView transitionFromView:self.tileView toView:tileView duration:DURATION options:UIViewAnimationOptionTransitionFlipFromLeft|UIViewAnimationOptionAllowAnimatedContent completion:^(BOOL finished) {
                    self.tileView = tileView;
                    complete(finished);
                }];
            });
        }
        else                    //del
        {
            DispatchMainThreadSyncAfterDelay(delayTime, ^{
                [UIView transitionFromView:self.tileView toView:self.placeHolderView duration:DURATION options:UIViewAnimationOptionTransitionFlipFromLeft|UIViewAnimationOptionAllowAnimatedContent completion:^(BOOL finished) {
                    self.tileView = nil;
                    complete(finished);
                }];
            });
        }
    }
    else
    {
        if (tileView != nil)    //add
        {
            DispatchMainThreadSyncAfterDelay(delayTime, ^{
                [UIView transitionFromView:self.placeHolderView toView:tileView duration:DURATION options:UIViewAnimationOptionTransitionFlipFromLeft|UIViewAnimationOptionAllowAnimatedContent completion:^(BOOL finished) {
                    self.tileView = tileView;
                    complete(finished);
                }];
            });
        }
    }
}

@end